package com.ruoyi.produce.base.notice.controller;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.produce.base.notice.domain.Notice;
import com.ruoyi.produce.base.notice.domain.NoticeCond;
import com.ruoyi.produce.base.notice.service.INoticeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 布告栏
 */
@RestController
@RequestMapping("/produce/notice")
@Api(value = "布告栏模块", tags = "布告栏接口")
public class NoticeController extends BaseController {
    @Autowired
    private INoticeService noticeService;

    /**
     * 获取布告栏
     * @param cond
     * @return
     */
    @ApiOperation(value = "获取布告栏列表",notes = "查询所有布告栏信息")
    @PostMapping("/list")
    public TableDataInfo list(@RequestBody NoticeCond cond) {
        startPage();
        List<Notice> noticeList = noticeService.selectNoticeList(cond);
        return getDataTable(noticeList);
    }

    /**
     * 导出布告栏
     *
     * @param cond
     * @return
     */
    @PostMapping("/export")
    public AjaxResult export(@RequestBody NoticeCond cond) {
        List<Notice> noticeList = noticeService.selectNoticeList(cond);
        ExcelUtil<Notice> util = new ExcelUtil<>(Notice.class);
        return util.exportExcel(noticeList, "模具信息");
    }

    /**
     * 新增布告栏
     * @param notice
     * @return
     */
    @ApiOperation(value = "新增布告栏", notes = "新增布告栏信息")
    @ApiImplicitParam(name = "notice", value = "新增布告栏", dataType = "Notice")
    @PostMapping("/add")
    public AjaxResult add(@RequestBody Notice notice) {
        try {
            noticeService.insertNotice(notice);
            return AjaxResult.success("新增成功");
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("新增失败");
        }
    }

    /**
     *
     * 更新布告栏
     * @param notice
     * @return
     */
    @ApiOperation(value = "修改布告栏", notes = "修改布告栏")
    @ApiImplicitParam(name = "notice", value = "修改布告栏", dataType = "Notice")
    @PostMapping("/edit")
    public AjaxResult edit(@RequestBody Notice notice) {
        try {
            noticeService.updateNotice(notice);
            return AjaxResult.success("修改成功");
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("修改失败");
        }
    }

    /**
     * 删除布告栏
     * 仅修改删除标志位
     * @param notice
     * @return
     */
    @ApiOperation(value = "删除布告栏", notes = "删除布告栏,仅删除标志位")
    @PostMapping("/delete")
    public AjaxResult delete(@RequestBody Notice notice) {

        return toAjax(noticeService.updateNoticeDeleteFlag(notice));
    }

    /**
     * 根据类型查看看板的布告栏返回近三天
     *
     * @return
     */
    @ApiOperation("根据类型查看看板的布告栏返回近三天")
    @GetMapping("/kanban")
    public AjaxResult selectNoticeListForKanBan(@RequestParam String noticeType) {
        List<Notice> notices = noticeService.selectNoticeListForKanBan(noticeType);
        return AjaxResult.success("查询成功", notices);
    }
}
